//
//  RechargeViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "RechargeViewController.h"
#import "HaveLogRechargeTableViewCell.h"
#import "CheckPhoneView.h"
#import "RechargeView.h"
@interface RechargeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * footerView;
@property (nonatomic, strong)RechargeView * rechargeView;
@property (nonatomic, strong)CheckPhoneView * checkPhoneView;//验证手机号
@property (nonatomic, strong)NSMutableArray * cellData;
@end

@implementation RechargeViewController(networking)
//判断是否绑定手机号
- (void)verifyPhoneNum{
    NSDictionary * dic = @{@"openid":@"88888888"};
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"plan/checkMobile" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"0"]) {
            [self.checkPhoneView show];
//            [self bindPhoneNum];
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"1"]){
            ShowMessage(responseObject[@"msg"]);
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"2"]){
            [self.cellData removeAllObjects];
            [self.cellData addObjectsFromArray:responseObject[@"list"]];
            [self.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end
@implementation RechargeViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.checkPhoneView = [[CheckPhoneView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT)];
//    self.checkPhoneView.bindPhoneNumBlock = ^{
//        [self bindPhoneNum];//网络接口
//    };
    [self verifyPhoneNum];//网络接口
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.checkPhoneView cancel];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.estimatedRowHeight = 114/WIDTH_6_SCALE;
//        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.rowHeight = 114/WIDTH_6_SCALE;
        [_tableView registerClass:[HaveLogRechargeTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HaveLogRechargeTableViewCell class])];
        _tableView.tableFooterView = self.footerView;
        _tableView.backgroundColor = getColor(@"f5f5f5");
    }return _tableView;
}
- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 70/WIDTH_6_SCALE)];
        _footerView.backgroundColor = getColor(@"f5f5f5");
        UIButton * chargeBtn = [[UIButton alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, SCREEN_WIDTH - 30/WIDTH_6_SCALE, _footerView.height - 30/WIDTH_6_SCALE)];
        [self button:chargeBtn withBGColor:getColor(@"4bceae") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:4 withMasksToBounds:YES withViwe:_footerView];
        [chargeBtn setTitle:@"选择充值金额" forState:UIControlStateNormal];
        [chargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        chargeBtn.titleLabel.font = DEF_FontSize_14;
        [chargeBtn addTarget:self action:@selector(chargeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [chargeBtn setBackgroundImage:[UIImage imageNamed:@"maself_bg"] forState:UIControlStateNormal];
    }return _footerView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[].mutableCopy;
    }return _cellData;
}
#pragma mark - action
- (void)chargeBtnClick{
    [self.rechargeView show];
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HaveLogRechargeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HaveLogRechargeTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellData.count;
}
@end
